package web.servlet.output;

import domain.PageBean;
import domain.Student;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import service.StudentService;
import service.impl.StudentServiceImpl;
import utils.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

@WebServlet("/outputServlet")
public class outputServlet extends HttpServlet{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
        String sql = "select * from student where s_class = ?";
        String s_class = "计科7班";
        List<Student> output = template.query(sql, new BeanPropertyRowMapper<Student>(Student.class), s_class);
        String path = req.getRealPath("/");
        BufferedWriter bw;
        try {
            bw = new BufferedWriter(new FileWriter(path + "StudentOutput.txt"));
            for (int j = 0; j < output.size(); j++) {
                bw.write("第" + (j + 1) + "名学生\t  学号:" + output.get(j).getS_id() + "\t姓名:" + output.get(j).getS_name() + "\t  学院:" + output.get(j).getS_department() +
                       "\t班级:" + output.get(j).getS_class() + "\t性别:" + output.get(j).getS_sex() + "\t  电话:" + output.get(j).getS_phone());
                bw.newLine();
                bw.flush();
            }
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


        req.getRequestDispatcher("findStudentByPageServlet").forward(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
